package com.example.wordpoitldemo.controller;

import com.deepoove.poi.XWPFTemplate;
import com.example.wordpoitldemo.WordService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;


/**
 * @author: xh
 * @date: 2023/11/15 9:48
 */
@Controller
@RequestMapping("/word")
public class WordController {

    @Resource
    private WordService wordService;

    @ApiOperation("Download Word")
    @GetMapping("download")
    public void wordDownload( HttpServletResponse response) {
        try {
            XWPFTemplate document =wordService.generateWordXWPFTemplate();
            String fileName="word文档导出.docx";
            response.reset();
            response.setContentType("text/html:charset=utf-8");
            String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"");
            OutputStream os = response.getOutputStream();
            document.write(os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
